package edu.unindra.raport.gui;

import javax.swing.AbstractAction;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;
import javax.swing.LayoutStyle.ComponentPlacement;

import edu.unindra.raport.dao.impl.OperatorImpl;
import edu.unindra.raport.template.About;
import edu.unindra.raport.template.PanelBackGround;
import edu.unindra.raport.template.PanelTransparan;
import usu.widget.ButtonGlass;
import usu.widget.Form;
import usu.widget.GlassPane;
import usu.widget.Label;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import usu.widget.constan.Animation;
import usu.widget.glass.PasswordBox;
import usu.widget.glass.TextBoxGlass;

import java.awt.Dimension;
import java.sql.SQLException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.awt.Color;
import java.awt.Font;

public class FormLogIn extends Form {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2471410853205547369L;
	private ExecutorService executor;
	private GlassPane glassPane;
	private PanelBackGround panelUtama;
	private PanelTransparan panelTransparan;

	private About about;

	private OperatorImpl opImpl;
	private FormMenu fu;

	private ButtonGlass btnReset;
	private ButtonGlass btnLogIn;

	private Label label1;
	private JLabel titleBody;
	private JLabel jLabel1;
	private JLabel jLabel2;
	private TextBoxGlass txtUserName;
	private PasswordBox txtPassword;

	/** Creates new form ELearningForm */
	public FormLogIn() {
		opImpl = new OperatorImpl();

		setResizable(false);
		executor = Executors.newCachedThreadPool();

		getContentPane().setEnabled(false);
		setAnimationShow(Animation.SHOW_FROM_BOTTOM);
		setAnimationHide(Animation.HIDE_TO_BOTTOM);
		setLocationRelativeTo(null);
		setTitle("Raport");
		setMinimumSize(new Dimension(450, 300));

		addWindowListener(new java.awt.event.WindowAdapter() {
			public void windowClosing(java.awt.event.WindowEvent evt) {
				formWindowClosing(evt);
			}
		});

		initComponents();
		getRootPane().getInputMap().put(
				KeyStroke.getKeyStroke((char) KeyEvent.VK_ESCAPE), "CLOSE");
		getRootPane().getActionMap().put("CLOSE", new AbstractAction() {

			private static final long serialVersionUID = -1;

			public void actionPerformed(ActionEvent e) {
				((GlassPane) getGlassPane()).hideComponent();
			}
		});

		fu = new FormMenu();
	}

	private void initComponents() {
		glassPane = new usu.widget.GlassPane();
		panelUtama = new PanelBackGround();
		panelTransparan = new PanelTransparan();

		btnReset = new ButtonGlass();
		btnReset.setGlassColor(Color.LIGHT_GRAY);
		btnReset.setForeground(Color.BLACK);
		btnReset.setFont(new Font("Tahoma", Font.BOLD, 12));
		btnLogIn = new ButtonGlass();
		btnLogIn.setGlassColor(Color.LIGHT_GRAY);
		btnLogIn.setForeground(Color.BLACK);
		btnLogIn.setFont(new Font("Tahoma", Font.BOLD, 12));

		titleBody = new JLabel();
		jLabel1 = new JLabel();
		jLabel2 = new JLabel();
		label1 = new Label();

		txtUserName = new TextBoxGlass();
		txtPassword = new PasswordBox();

		about = new About();
		about.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				aboutActionPerformed(evt);
			}
		});

		setGlassPane(glassPane);
		glassPane.setAlphaDisable(10);
		glassPane.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.CENTER,
				100, 100));

		setTitle("Raport - Aplikasi");
		setAnimationHide(usu.widget.constan.Animation.HIDE_TO_BOTTOM);
		setAnimationShow(usu.widget.constan.Animation.SHOW_FROM_LEFT);
		setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
		setDefaultButton(btnLogIn);
		setResizable(false);

		titleBody.setFont(new Font("Arial Unicode MS", Font.BOLD, 30));
		titleBody.setForeground(Color.BLACK);
		titleBody.setText("Raport");

		panelTransparan.setAlpha(0.0F);

		label1.setVerticalAlignment(javax.swing.SwingConstants.TOP);
		label1.setIconReflec(new javax.swing.ImageIcon(getClass().getResource(
				"/edu/unindra/raport/image/operator.png"))); // NOI18N

		jLabel1.setDisplayedMnemonic('U');
		jLabel1.setFont(new Font("Tahoma", Font.BOLD, 14)); // NOI18N
		jLabel1.setForeground(Color.BLACK);
		jLabel1.setText("Username");

		jLabel2.setDisplayedMnemonic('P');
		jLabel2.setFont(new Font("Tahoma", Font.BOLD, 14));
		jLabel2.setForeground(Color.BLACK);
		jLabel2.setText("Password");

		usu.widget.util.WidgetUtilities.setAutomaticPopUpMenu(txtUserName);
		txtUserName.setForeground(Color.BLACK);
		txtUserName.setHorizontalAlignment(javax.swing.JTextField.LEFT);
		txtUserName.setToolTipText("Masukkan username");
		txtUserName.setFocusAccelerator('U');
		txtUserName.setFont(new java.awt.Font("Tahoma", 1, 11));

		txtPassword.setForeground(Color.BLACK);
		txtPassword.setHorizontalAlignment(javax.swing.JTextField.LEFT);
		txtPassword.setToolTipText("Masukkan password");
		txtPassword.setFocusAccelerator('P');
		txtPassword.setFont(new java.awt.Font("Tahoma", 1, 11));

		btnLogIn.setMnemonic('M');
		btnLogIn.setText("Masuk");
		btnLogIn.setToolTipText("Klik untuk masuk ke form utama");
		btnLogIn.setRoundRect(true);
		btnLogIn.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				try {
					buttonLogInActionPerformed(evt);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});

		btnReset.setMnemonic('R');
		btnReset.setText("Reset");
		btnReset.setToolTipText("Klik untuk mereset textbox username dan password");
		btnReset.setRoundRect(true);
		btnReset.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				buttonResetActionPerformed(evt);
			}
		});

		javax.swing.GroupLayout panelTranparanLayout = new javax.swing.GroupLayout(
				panelTransparan);
		panelTransparan.setLayout(panelTranparanLayout);
		panelTranparanLayout
				.setHorizontalGroup(panelTranparanLayout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								panelTranparanLayout
										.createSequentialGroup()
										.addContainerGap()
										.addComponent(
												label1,
												javax.swing.GroupLayout.PREFERRED_SIZE,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												javax.swing.GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												panelTranparanLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.TRAILING)

														.addGroup(
																panelTranparanLayout
																		.createSequentialGroup()
																		.addGroup(
																				panelTranparanLayout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.LEADING,
																								false)
																						.addComponent(
																								jLabel2,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								Short.MAX_VALUE)
																						.addComponent(
																								jLabel1,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								62,
																								Short.MAX_VALUE))
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addGroup(
																				panelTranparanLayout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.LEADING)
																						.addComponent(
																								txtUserName,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								304,
																								Short.MAX_VALUE)
																						.addComponent(
																								txtPassword,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								304,
																								Short.MAX_VALUE)))
														.addGroup(
																panelTranparanLayout
																		.createSequentialGroup()
																		.addComponent(
																				btnReset,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				javax.swing.GroupLayout.DEFAULT_SIZE,
																				javax.swing.GroupLayout.PREFERRED_SIZE)
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addComponent(
																				btnLogIn,
																				javax.swing.GroupLayout.PREFERRED_SIZE,
																				javax.swing.GroupLayout.DEFAULT_SIZE,
																				javax.swing.GroupLayout.PREFERRED_SIZE)))
										.addContainerGap()));
		panelTranparanLayout
				.setVerticalGroup(panelTranparanLayout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								panelTranparanLayout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												panelTranparanLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(
																label1,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																283,
																Short.MAX_VALUE)
														.addGroup(
																panelTranparanLayout
																		.createSequentialGroup()
																		.addGroup(
																				panelTranparanLayout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.BASELINE)
																						.addComponent(
																								jLabel1)
																						.addComponent(
																								txtUserName,
																								javax.swing.GroupLayout.PREFERRED_SIZE,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								javax.swing.GroupLayout.PREFERRED_SIZE))
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
																		.addGroup(
																				panelTranparanLayout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.BASELINE)
																						.addComponent(
																								jLabel2)
																						.addComponent(
																								txtPassword,
																								javax.swing.GroupLayout.PREFERRED_SIZE,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								javax.swing.GroupLayout.PREFERRED_SIZE))
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																		.addGroup(
																				panelTranparanLayout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.BASELINE)
																						.addComponent(
																								btnLogIn,
																								javax.swing.GroupLayout.PREFERRED_SIZE,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								javax.swing.GroupLayout.PREFERRED_SIZE)
																						.addComponent(
																								btnReset,
																								javax.swing.GroupLayout.PREFERRED_SIZE,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								javax.swing.GroupLayout.PREFERRED_SIZE))
																		.addPreferredGap(
																				javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)))
										.addContainerGap()));

		javax.swing.GroupLayout panelUtamaLayout = new javax.swing.GroupLayout(
				panelUtama);
		panelUtamaLayout
				.setHorizontalGroup(panelUtamaLayout
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								panelUtamaLayout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												panelUtamaLayout
														.createParallelGroup(
																Alignment.LEADING)
														.addComponent(
																panelTransparan,
																GroupLayout.DEFAULT_SIZE,
																534,
																Short.MAX_VALUE)
														.addComponent(titleBody))
										.addContainerGap()));
		panelUtamaLayout.setVerticalGroup(panelUtamaLayout.createParallelGroup(
				Alignment.LEADING).addGroup(
				panelUtamaLayout
						.createSequentialGroup()
						.addContainerGap()
						.addComponent(titleBody)
						.addPreferredGap(ComponentPlacement.RELATED)
						.addComponent(panelTransparan,
								GroupLayout.PREFERRED_SIZE, 215,
								GroupLayout.PREFERRED_SIZE)
						.addContainerGap(116, Short.MAX_VALUE)));
		panelUtama.setLayout(panelUtamaLayout);

		getContentPane().add(panelUtama, java.awt.BorderLayout.CENTER);

		java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit()
				.getScreenSize();
		setBounds((screenSize.width - 560) / 2, (screenSize.height - 418) / 2,
				560, 309);

	}

	private void aboutActionPerformed(java.awt.event.ActionEvent evt) {// GEN-FIRST:event_aboutActionPerformed
		((GlassPane) getGlassPane()).hideComponent();
	}

	@SuppressWarnings("deprecation")
	private void buttonLogInActionPerformed(java.awt.event.ActionEvent evt)
			throws Exception {
		if (opImpl.login(txtUserName.getText().toString(), txtPassword
				.getText().toString())) {
			this.setVisible(false);
			fu.setVisible(true);
		} else {
			JOptionPane.showMessageDialog(this,
					"Username/id dan password tak ditemukan\n"
							+ "\nSilahkan hubungi administrator anda\n",
					"Error", JOptionPane.ERROR_MESSAGE);
		}
	}

	private void buttonResetActionPerformed(java.awt.event.ActionEvent evt) {
		reset();
	}

	public void reset() {
		txtPassword.setText("");
		txtUserName.setText("");
		txtUserName.requestFocusInWindow();
	}

	private void formWindowClosing(java.awt.event.WindowEvent evt) {// GEN-FIRST:event_formWindowClosing
		exit();
	}

	@Override
	public void exit() {
		if (showConfirm("Anda Yakin?") != JOptionPane.OK_OPTION) {
			return;
		}
		executor.shutdown();
		super.exit();
	}

	private int showConfirm(String message) {
		return JOptionPane.showConfirmDialog(this, message, "Konfirmasi",
				JOptionPane.OK_CANCEL_OPTION);
	}

}
